package com.hzdl.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.hotel.service.CommentService;
import com.hzdl.hotel.util.AJAXResult;
import com.hzdl.hotel.vo.AdviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/advice")
public class AdviceController {

    @Autowired
    private CommentService cs;

    @RequestMapping("/adviceShowInit")
    public String adviceShowInit(){
        return "administer/adviceManage";
    }

    @RequestMapping("/adviceShow")
    @ResponseBody
    public AJAXResult adviceShow(AdviceVo adviceVo){
        if (adviceVo.getPage() == null) {
            adviceVo.setPage(1);
        }
        if (adviceVo.getLimit() == null) {
            adviceVo.setLimit(5);
        }
        PageInfo pageInfo = cs.findAdviceByWhere(adviceVo);
        return new AJAXResult(0, "", pageInfo.getList(), pageInfo.getTotal());
    }

    @RequestMapping("/adviceDelete")
    @ResponseBody
    public AJAXResult adviceDelete(Integer adviceId){
        int row= cs.deleteAdviceById(adviceId);
        if(row!=0){
            return AJAXResult.DELETE_SUCCESS;
        }else{
            return AJAXResult.DELETE_ERROR;
        }
    }

    @RequestMapping("/deleteAllAdvice")
    @ResponseBody
    public AJAXResult deleteAllAdvice(String ids){
        String[] id= ids.split(",");
        int row=0;
        for(int i=0;i<id.length;i++){
            row+=cs.deleteAdviceById(Integer.valueOf(id[i]));
        }
        if(row==0){
            return AJAXResult.DELETE_ERROR;
        }else{
            return AJAXResult.DELETE_SUCCESS;
        }
    }
}
